<?php
/*
 * @desc:	新版网上商城的接口
 * @author:	cpj
 * @date:	2012-3-28
 * @copyright:	dld.com
 * 
 * @params:	方法返回的数据格式均为:	data = array(
 * 												'page' => array('pageSize' => xx,'pageCount' => xx),
 * 												'data' = array()
 * 												)
 * 相应的商城API代码在:商城目录/dld/
 * 
 */
class NewMallApi 
{
	private $_mallHost = '';
	private $_sKey = '4b0b06e2966ec27815f83ebdaa195aa9';
	
	
	public function __construct()
	{
		$this->connect();
	}
	
	private function connect()
	{
		$mallHost = Yii::app()->params['newMallUrl'];
		$res = get_headers($mallHost);
		if($res)
		{
			$this->_mallHost = $mallHost;
			return TRUE;
		}
		else
		{
			Throw new Exception('Connecting to mall.dld.com Fail!',404);
		}
	}
	
	/**
	 * 同步帐户到商城
	 * params:	$other = array('msn', 'qq', 'office_phone', 'home_phone', 'mobile_phone');
	 *          $flag = 0 | 1     1代表主站注册   0 代表商城注册    功能：避免用户注册时重复添加积分  add by  yt 2012-08-09
	 */
	public function syncUser($username,$password,$email,$other = array(),$flag = 0)
	{
		for($i = 0;$i < 3;$i++)
		{
			try{
			$url = $this->_mallHost . '/dld/act.php?act=adduser';
			$data['user'] = $username;
			$data['password'] = $password;
			$data['email'] = $email;
			$data['other'] = serialize($other);
			$data['flag'] = $flag;
			$res = $this->get($url,$data);
			break;
			}catch(Exception $e)
			{
				sleep(1);
				continue;
			}
		}
		return $res;
	}
	
	/**
	 * 检查该同步是否已经在商城存在,不存在便同步
	 */
	public function checkUser($username,$password,$email,$other = array(),$flag=1)
	{
		$url = $this->_mallHost . '/dld/act.php?act=checkuser';
		$data['user'] = $username;
		$data['password'] = $password;
		$data['email'] = $email;
		$data['other'] = serialize($other);
		$data['flag'] = $flag;
		$res = $this->get($url,$data);
		return $res;
	}
	
	/**
	 * 升级用户(普通用户=>店连店会员卡用户)
	 */
	public function upgradeRank($username)
	{
		$url = $this->_mallHost . '/dld/act.php?act=updateRank';
		$data['user'] = $username;
		$res = $this->get($url,$data);
		return $res;
	}
	
	/**
	 * 更新用户信息
	 */
	public function updateProfile($username,$profile = array())
	{
		if(!count($profile))
		{
			return FALSE;
		}
		$url = $this->_mallHost . '/dld/act.php?act=updateProfile';
		return $this->get($url,$profile);
	}
	
	/**
	 * 订单检测
	 */
	public  function checkOrderInfo($username, $order_sn, $money){
/*		$username		= 'zengdandan';
		$order_sn		= '2013041966520';
		$money			= 39;*/
		$url 			=  $this->_mallHost . '/dld/act.php?act=checkorder';
		$data['order']	= sprintf('username=%s&order_sn=%s&money=%d', $username, $order_sn,$money);
		$res = $this->get($url,$data);
		return $res;
	}
	
	/**
	 *调用商城热销商品及广告至主站首页
	 */
	public  function getHotGoodsToMain(){
		$url = $this->_mallHost.'/dld/act.php?act=hot_goods';
		$res = $this->get($url);
		return $res;
	}
	
	/**
	 * 取得url的数据
	 */
	private function get($url,$params = array())
	{
		$src = $url . '&' . $this->deData($params);
		$data = file_get_contents($src);
		return json_decode($data);
	}
	
	
	/**
	 * 加密数据
	 */
	private function deData($data,$key = '')
	{
		if(empty($key))
		{
			$key = $this->_sKey;
		}
		$nData = array();
		foreach($data as $k => $v)
		{
			$nData[$k] = XxteaD::encrypt($v, $key);
		}
		$dataStr = '';
		foreach($nData as $k2 => $v2)
		{
			$dataStr .= $k2 . '=' . $v2 . '&';
		}
		return $dataStr;
	}
	
	
}